package com.kaisu.blog.application.file.service.impl;

import com.kaisu.blog.application.file.convert.FileApplicationConvert;
import com.kaisu.blog.application.file.dto.FileDTO;
import com.kaisu.blog.application.file.service.FileService;
import com.kaisu.blog.application.user.util.UserUtil;
import com.kaisu.blog.domain.file.File;
import com.kaisu.blog.domain.file.repository.FileRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * @author kaisu
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FileServiceImpl implements FileService {
    private final FileApplicationConvert fileConvert;
    private final FileRepository fileRepository;

    @Override
    public FileDTO uploadFile(InputStream fileStream, String fileName) {
        File file = File.create(fileName, UserUtil.currentUser());
        fileRepository.upload(fileStream, file);
        return fileConvert.toDTO(file);
    }

    public FileDTO findById(String id) {
        return fileConvert.toDTO(fileRepository.findById(id));
    }
}
